package com.digigraphic.article.action;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.inject.Inject;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import net.sf.json.JSONObject;

import com.digigraphic.article.service.ArticleService;
import com.digigraphic.entity.Article;
import com.digigraphic.entity.ArticleClass;
import com.digigraphic.entity.BaseInfo;
import com.digigraphic.entity.ChildPage;
import com.digigraphic.entity.DetailPage;
import com.digigraphic.entity.File;
import com.digigraphic.entity.User;
import com.digigraphic.mainpage.service.MainpageService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

/**
 * version：
 * 
 * @replaceTime:
 * @replaceContext：
 * 
 * 
 * 
 * @author：Royal
 * @classInfo: 文章后台处理
 */

public class ArticleAction extends ActionSupport implements ModelDriven<Article>{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private ChildPage childPage = new ChildPage();							

	private DetailPage detailPage = new DetailPage();		
	
	private List<ArticleClass> classList = new ArrayList<ArticleClass>();
	
	//此ID为文章分类ID
	private int classId;
	//此id为Article的Id
	private int articleId;
	
	private int parentId;
	
	private String className;
	
	private String cName;
	
	private Article article = new Article();
								
	private int pageNum;
	
	//JSON传值用
	private String result;
	
	//文章分类,页面后台传值用
	private String articleClazz;
	
	//图片新闻缩略图存放路径
	private String picturePath;
	
	
	@Inject							
	private ArticleService service;	
	@Inject
	private MainpageService mps;

	@Override
	public String execute() throws Exception {
		return SUCCESS;
	}
	
	
	//取得文章TITLE列表的方法
	public String list() throws UnsupportedEncodingException{
		List<Integer> pageNumInfoList = new ArrayList<Integer>();
		//该分类的记录总数
		int totalResultCount = 0;		
		//总页数
		int pageCount = 0;	
		//每页记录数
		int perPageCount = 10;						
		List titleList = new ArrayList();
		
		 System.out.println(cName);
		//取得当前页的10条记录
		titleList = service.getTitleList(pageNum,classId,parentId);						
		
		//取得分页需参数
		pageNumInfoList = service.getPageNumInfo(perPageCount,classId,parentId);						
		totalResultCount = pageNumInfoList.get(0);						
		pageCount = pageNumInfoList.get(1);		
		
		
		
		//放入传页面对象						
		childPage.setShowArticle(titleList);						
		childPage.setResultCount(totalResultCount);						
		childPage.setPerPageResultCount(perPageCount);						
		childPage.setPageCount(pageCount);
		
//		判断名称是否为空，防止页面显示空的分类名称
		if( cName == null || cName.trim() == ""){
			cName = "";
		}else{
			cName = java.net.URLDecoder.decode(cName,"UTF-8");
		}
		
		childPage.setCurrentPosition(cName);
		childPage.setClassId(classId);
		childPage.setActionName("listArticle");
		
		
		List<BaseInfo> mainPageInfoList = mps.getMPElementList();
		childPage.setTitleMap(mps.getChildMap(mainPageInfoList));
		if (pageNum == 0) {						
			pageNum = 1;					
		}						
		
		
		
		childPage.setCurPage(pageNum);	
		return SUCCESS;		
	}
	
	//三级页面的后台
	public String articleDtl() throws Exception{
		List detailList = new ArrayList();
		String detailPosition = "";
		if( cName == null || cName.trim() == ""){
			cName = "";
		}else{
			cName = java.net.URLDecoder.decode(cName,"UTF-8");
		}
		detailList = service.getDetailList(articleId);
		for (int i = 0; i < detailList.size(); i++) {
			Article art = (Article) detailList.get(i);
			detailPosition = art.getClassName();
			if(!detailPosition.equals(cName)&& !cName.equals("")){
				detailPosition= cName+" >> "+detailPosition;
			}
		}
		detailPage.setShowArticle(detailList);
		detailPage.setCurrentPosition(detailPosition);
		
		List<BaseInfo> mainPageInfoList = mps.getMPElementList();
		detailPage.setTitleMap(mps.getChildMap(mainPageInfoList));
		
		return SUCCESS;
	}
								
	public ChildPage getChildPage() {							
		return childPage;						
	}							
								
	public void setChildPage(ChildPage childPage) {							
		this.childPage = childPage;						
	}							

	public DetailPage getDetailPage() {							
		return detailPage;						
	}							

	public void setDetailPage(DetailPage detailPage) {							
		this.detailPage = detailPage;						
	}							

	public int getPageNum() {							
		return pageNum;						
	}							

	public void setPageNum(int pageNum) {	
		this.pageNum = pageNum;
	}


	public int getClassId() {
		return classId;
	}

	public void setClassId(int classId) {
		this.classId = classId;
	}


	public Article getModel() {
		//通过Struts传过来的Article对象
		return article;
	}

	public int getArticleId() {
		return articleId;
	}

	public void setArticleId(int articleId) {
		this.articleId = articleId;
	}

	public Article getArticle() {
		return article;
	}

	public void setArticle(Article article) {
		this.article = article;
	}


	public List<ArticleClass> getClassList() {
		return classList;
	}


	public void setClassList(List<ArticleClass> classList) {
		this.classList = classList;
	}


	public String getResult() {
		return result;
	}


	public void setResult(String result) {
		this.result = result;
	}


	public String getArticleClazz() {
		return articleClazz;
	}


	public void setArticleClazz(String articleClazz) {
		this.articleClazz = articleClazz;
	}


	public String getPicturePath() {
		return picturePath;
	}


	public void setPicturePath(String picturePath) {
		this.picturePath = picturePath;
	}




	public int getParentId() {
		return parentId;
	}


	public void setParentId(int parentId) {
		this.parentId = parentId;
	}


	public String getClassName() {
		return className;
	}


	public void setClassName(String className) {
		this.className = className;
	}


	public String getCName() {
		return cName;
	}


	public void setCName(String name) {
		cName = name;
	}





}
